let gulp = require('gulp');
let webserver = require('gulp-webserver');
let htmlmin = require('gulp-htmlmin');
let cssmin = require('gulp-cssmin');
let gulpclean = require('gulp-clean');
let jsmin = require("gulp-uglify");
let gulpBable = require("gulp-babel");

function html() {
    return gulp.src("./src/html/**").pipe(htmlmin(
        {
                collapseWhitespace: true, // 表示去除空格
                removeEmptyAttributes: true, // 移出空的属性
                minifyCSS: true, // 压缩 style 标签
                minifyJS: true, // 压缩 script 标签
            }
    )).pipe(gulp.dest("./dist/html"));
}


function css() {
    return gulp.src("./src/css/**")
        .pipe(cssmin())
        .pipe(gulp.dest("./dist/css"));
}

function js() {
    return (
        gulp
            .src("./src/js/**")
            // 把es6 es7 的代码转换为es5 的代码
            .pipe(
                gulpBable({
                    presets: ["env"],
                })
            )
            //在进行压缩
            .pipe(jsmin())
            .pipe(gulp.dest("./dist/js"))
    );
}

function images() {
    return gulp.src("./src/images/**").pipe(gulp.dest("./dist/images"));
}
function data() {
    return gulp.src("./src/data/**").pipe(gulp.dest("./dist/data"));
}
function watch() {
    gulp.watch('./src/html', html);
    gulp.watch('./src/css', css);
    gulp.watch('./src/js', js);
    gulp.watch('./src/images', images);
}
function clear() {
    return gulp.src(["./dist"]).pipe(gulpclean());
 }
function web() {
    return gulp.src("./dist").pipe(
        webserver({
            host: "localhost",
            port: 3000,
            open: "./html/huaindex.html", 
            livereload: true,
            proxies: [
                {
                    source: "/flo",
                    target:"http://localhost/hua/api/",
                }
            ]
        })
    )
}

exports.html = html;
exports.web = web;
exports.css = css;
exports.images = images;
exports.js = js;
exports.data = data;

exports.build = gulp.series(clear, gulp.parallel(css, html,js, images,data), web, watch);